<!-- 回复管理 -->
<template>
	<div>
		<el-breadcrumb :separator-icon="ArrowRight">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>心理答疑管理</el-breadcrumb-item>
			<el-breadcrumb-item>回复管理</el-breadcrumb-item>
		</el-breadcrumb>
		<hr />


		<!-- 头部搜索栏 -->
		<!-- 实例是：formInline -->
		<div>
			<el-form :inline="true" :model="formInline" class="demo-form-inline">
				<el-form-item label="回复内容">
					<el-input v-model="formInline.replyContent" placeholder="请输入回复内容" clearable
						@clear="handleClear1()" />
				</el-form-item>
				<el-form-item label="答疑问题">
					<el-input v-model="formInline.problemTitle" placeholder="请输入问题" clearable @clear="handleClear2()" />
				</el-form-item>
				<el-form-item label="回复人昵称">
					<el-input v-model="formInline.nickname" placeholder="请输入回复人昵称" clearable @clear="handleClear3()" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getData()">搜索</el-button>
					<el-button type="danger" @click="delIds()">批量删除</el-button>
				</el-form-item>

			</el-form>


			<!-- 查看与删除 -->
			<!-- 实例是editForm -->
			<el-dialog v-model="edit"  title="回复详情" >
				<el-form :model="editForm" label-width="auto" label-position='right'>
					<el-form-item label="提问人昵称">
						<el-input v-model="editForm.problem.nickname" readonly />
					</el-form-item>
					<el-form-item label="问题标题">
						<el-input v-model="editForm.problem.problemTitle" readonly />
					</el-form-item>
					<el-form-item label="问题内容" >
						<el-input v-model="editForm.problem.problemContent" type="textarea" rows="10" resize='none' readonly />
					</el-form-item>
					<el-form-item label="回复人昵称">
						<el-input v-model="editForm.sysUser.nickname" readonly/>
					</el-form-item>
					
					<el-form-item label="回复内容">
						<el-input  type="textarea" rows="10" resize='none' readonly v-model="editForm.replyContent"/>
					</el-form-item>
				</el-form>
				<template #footer>
					<div class="dialog-footer">
						<el-button @click="editNo()">关闭</el-button>
					</div>
				</template>
			</el-dialog>


		</div>


		<!-- 表格数据展示 -->
		<!-- 实例是tableData -->
		<div>
			<el-table :data="tableData" border @selection-change="handleSelectionChange"
				style="width: 90%;margin-left: 30px;">
				<el-table-column :selectable="selectable" type="selection" width="55" />
				<el-table-column label="ID" width="120">
					<template #default="scope">{{ scope.row.id }}</template>
				</el-table-column>
				<el-table-column property="replyContent" label="回复内容" />
				<el-table-column show-overflow-tooltip width="250px" property="replyTime" label="回复时间" />
				<el-table-column property="sysUser.nickname" label="回复人" />
				<el-table-column property="problem.problemTitle" label="回复的问题" />


				<el-table-column label="操作">
					<template #default="scope">
						<el-button type="primary" @click="handleEdit(scope.$index, scope.row)">
							查看
						</el-button>
						<el-button type="danger" @click="handleDelete(scope.$index, scope.row)">
							删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>


		<!-- 分页设置 -->
		<div>
			<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[5, 10, 15, 20]"
				:size="size" :disabled="disabled" :background="background"
				layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
				@current-change="handleCurrentChange" />
		</div>
	</div>
</template>

<script setup>
	import {
		getCurrentInstance,
		onMounted,
		reactive,
		ref
	} from 'vue'
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'
	import {
		Plus
	} from '@element-plus/icons-vue'


	// 设置表单元素接收数据
	const formInline = reactive({
		problemTitle:'',
		nickname:'',
		replyContent:''
	})

	// 设置表格接收数据
	const tableData = reactive([{
		id: 0,
		replyContent:'',
		replyTime:'',
		userId:'',
		problemId:'',
		del:'',
		problem:{},
		nickname:'',
		problemTitle:'',
		sysUser:{}
	}])


	// 页面挂载后加载数据
	onMounted(() => {
		getData()
	})

	// 清除输入框自动搜索
	const handleClear1 = () => {
		formInline.replyContent = ''
		getData()
	};
	const handleClear2 = () => {
		formInline.problemTitle = ''
		getData()
	};
	const handleClear3 = () => {
		formInline.nickname = ''
		getData()
	};


	// 查看与删除
	const edit = ref(false)
	const editForm = reactive({
		id:'',
		replyContent:'',
		replyTime:'',
		userId:'',
		problemId:'',
		del:'',
		problem:{},
		nickname:'',
		problemTitle:'',
		sysUser:{}

	})
	const handleEdit = (index, row) => {
		edit.value = true
		// console.log(index, row)
		axios.get(`reply/${row.id}`).then((resp) => {
			console.log(resp);
			Object.assign(editForm, resp.data);
			// editForm.evaluationClassifyId = resp.data.evaluationClassify.classifyName
		})
	}
	const editNo = () => {
		edit.value = false;
		ElMessage('取消查看')
	}
	const handleDelete=(index,row)=>{
		console.log(row.id);
		ElMessageBox.confirm(
				'此操作将永久删除该文件, 是否继续?',
				'提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
				}
			)
			.then(() => {
				ids.value = multipleSelection.value.map(row => row.id).join(',')
				console.log(ids.value);
				axios.delete(`reply/${row.id}`).then((resp) => {
						console.log(resp);
						if (resp.code == 200) {
							ElMessage({
								type: 'success',
								message: resp.message,
							})
						} else {
							ElMessage.error(resp.message);
						}
					})
					.catch((error) => {
						console.error('Error:', error);
						ElMessage.error('请求失败，请稍后再试');
					})
					.finally(() => {
						getData()
					})
			})
			.catch(() => {
				ElMessage({
					type: 'info',
					message: '已取消删除',
				})
			})
	}

	// 批量删除
	const multipleSelection = ref([])
	const ids = ref('')
	const delIds = () => {
		if (multipleSelection.value.length == 0) {
			ElMessage('请选择要删除的数据！')
		} else {
			ElMessageBox.confirm(
					'此操作将永久删除该数据, 是否继续??',
					'提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				)
				.then(() => {
					ids.value = multipleSelection.value.map(row => row.id).join(',')
					console.log(ids.value);
					axios.delete(`reply?ids=${ids.value}`).then((resp) => {
							console.log(resp);
							if (resp.code == 200) {
								ElMessage({
									type: 'success',
									message: resp.message,
								})
							} else {
								ElMessage.error(resp.message);
							}
						})
						.catch((error) => {
							console.error('Error:', error);
							ElMessage.error('请求失败，请稍后再试');
						})
						.finally(() => {
							getData()
						})
				})
				.catch(() => {
					ElMessage({
						type: 'info',
						message: '取消删除',
					})
				})
		}
	}
	const handleSelectionChange = (val) => {
		multipleSelection.value = val
	}


	// 设置分页
	const currentPage = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const handleSizeChange = (val) => {
		console.log(`${val} items per page`)
		pageSize.value = val
		getData()
	}
	const handleCurrentChange = (val) => {
		console.log(`current page: ${val}`)
		currentPage.value = val
		getData()
	}


	// 获取axios对象实例发送请求
	const axios = getCurrentInstance().appContext.config.globalProperties.axios;
	// 获取表单数据
	const getData = () => {
		axios.get(
				`reply/page?pageNum=${currentPage.value}&pageSize=${pageSize.value}&problemTitle=${formInline.problemTitle}&nickname=${formInline.nickname}&replyContent=${formInline.replyContent}`
			)
			.then((resp) => {
				total.value = resp.data.total
				console.log(resp.data);

				tableData.splice(0, tableData.length, ...resp.data.list);
			})
	}
</script>

<style scoped>
	.demo-form-inline .el-input {
		--el-input-width: 220px;
	}

	.demo-form-inline .el-select {
		--el-select-width: 220px;
	}

.custom-height {
  height: 300px; /* 设置自定义高度 */
}
</style>